package main

import (
	"fmt"
	"github.com/urfave/cli"
	"log"
	"os"
)

func main() {
	app := cli.NewApp()

	tasks := []string{"cook", "clean", "laundry", "eat", "sleep", "code"}
	cli.VersionFlag = cli.BoolFlag{
		Name:  "version, V",
		Usage: "print only the version",
	}
	app.Name = "mygreet"
	app.Version = "19.99.0"

	app.UseShortOptionHandling = true
	app.EnableBashCompletion = true
	app.Flags = []cli.Flag{
		cli.BoolFlag{
			Name:  "ginger-crouton",
			Usage: "Add ginger croutons to the soup",
		},
	}
	app.Action = func(ctx *cli.Context) error {
		if !ctx.Bool("ginger-crouton") {
			return cli.NewExitError("Ginger croutons are not in the soup", 86)
		}
		return nil
	}
	app.Commands = []cli.Command{
		{
			Name:     "short",
			Usage:    "complete a task on the list",
			Category: "Task actions",
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "serve, s"},
				cli.BoolFlag{Name: "option, o"},
				cli.StringFlag{Name: "message, m"},
			},
			Action: func(c *cli.Context) error {
				fmt.Println("serve:", c.Bool("serve"))
				fmt.Println("option:", c.Bool("option"))
				fmt.Println("message:", c.String("message"))
				return nil
			},
		},
		{
			Name: "noop",
		},
		{
			Name:     "add_template",
			Category: "Template actions",
		},
		{
			Name:     "remove_template",
			Category: "Template actions",
		},
		{
			Name:     "add",
			Aliases:  []string{"a"},
			Usage:    "add a task to the list",
			Category: "Task actions",
			Action: func(c *cli.Context) error {
				fmt.Println("added task: ", c.Args().First())
				return nil
			},
		},
		{
			Name:     "complete",
			Category: "Task actions",
			Aliases:  []string{"c"},
			Usage:    "complete a task on the list",
			Action: func(c *cli.Context) error {
				fmt.Println("completed task: ", c.Args().First())
				return nil
			},
			BashComplete: func(c *cli.Context) {
				// This will complete if no args are passed
				if c.NArg() > 0 {
					return
				}
				for _, t := range tasks {
					fmt.Println(t)
				}
			},
		},
		{
			Name:     "template",
			Aliases:  []string{"t"},
			Usage:    "options for task templates",
			Category: "Task actions",
			Subcommands: []cli.Command{
				{
					Name:  "add",
					Usage: "add a new template",
					Action: func(c *cli.Context) error {
						fmt.Println("new task template: ", c.Args().First())
						return nil
					},
				},
				{
					Name:  "remove",
					Usage: "remove an existing template",
					Action: func(c *cli.Context) error {
						fmt.Println("removed task template: ", c.Args().First())
						return nil
					},
				},
			},
		},
	}
	err := app.Run(os.Args)
	if err != nil {
		log.Fatal(err)
	}
}
